package com.yequan.designpattern.singleton;

/**
 * @Auther: yq
 * @Date: 2019/1/11 12:46
 * @Description:
 */
public class StaticBlockSingleton {

    /**
     * 静态代码块实现单例模式（线程安全）
     */
    private static StaticBlockSingleton instance = null;

    private StaticBlockSingleton() {
    }

    static {
        try {
            Thread.sleep(300);
            instance = new StaticBlockSingleton();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static StaticBlockSingleton getInstance() {
        return instance;
    }

    public static void main(String[] args) {
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread1 >>>>> " + StaticBlockSingleton.getInstance().hashCode());
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread2 >>>>> " + StaticBlockSingleton.getInstance().hashCode());
            }
        });
        Thread thread3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("thread3 >>>>> " + StaticBlockSingleton.getInstance().hashCode());
            }
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }

}
